package com.power.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.power.common.PowerConstant;
import com.power.entity.*;
import com.power.service.ElectricPowerService;
import com.power.service.SystemProcessLogService;
import com.power.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


/**
 * @author Lijunxiong
 */
@Service
@Slf4j
public class ElectricPowerServiceImpl implements ElectricPowerService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Resource(name = "primaryRedisUtil")
    private RedisUtil primaryRedisUtil;


    @Resource
    private SystemProcessLogService systemProcessLogService;

    @Override
    public void addPowerToRedis(String valueOf, String valueOf2, String powerToHighAndLows, String dataType) {

        //此部分数据直接存入mongodb
        //String[] strArray = new String[]{"16440", "16441", "16442", "16453", "16454", "16455"};
        //String[] strArray = new String[]{"16659", "16660", "16661", "16666", "16667", "16668"};
        //高压
        if ("High".equals(powerToHighAndLows)) {
            String[] strArray = new String[]{"16443", "16456", "16676"};
            for (String string : strArray) {
                if (string.equals(valueOf)) {
                    HighRemoteDetect remoteDetect = new HighRemoteDetect();
                    remoteDetect.setRemotDetectId(Integer.parseInt(valueOf));
                    remoteDetect.setEValue(Double.valueOf(valueOf2));
                    remoteDetect.setCreateDate(new Date());
                    remoteDetect.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
                    mongoTemplate.save(remoteDetect, "yjf_e104_high_remote_detect_mutation");
                }
            }
        } else {
            //低压
            if ("16391".equals(valueOf)) {
                LowRemoteDetect remoteDetect = new LowRemoteDetect();
                remoteDetect.setRemotDetectId(Integer.parseInt(valueOf));
                remoteDetect.setEValue(Double.valueOf(valueOf2));
                remoteDetect.setCreateDate(new Date());
                remoteDetect.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
                mongoTemplate.save(remoteDetect, "yjf_e104_low_remote_detect_mutation");
            }
        }

        //根据电力类型存入redis
        if (primaryRedisUtil.hasKey(powerToHighAndLows + dataType)) {
            Map<Object, Object> hmget = primaryRedisUtil.hmget(powerToHighAndLows + dataType);
            hmget.put(valueOf, valueOf2.replace("\"", ""));
            primaryRedisUtil.hmset(powerToHighAndLows + dataType, hmget);

        } else {
            Map<Object, Object> map = new HashMap<Object, Object>();
            map.put(valueOf, valueOf2.replace("\"", ""));
            primaryRedisUtil.hmset(powerToHighAndLows + dataType, map);
        }

        /**
         * 新增redis插入时间
         * redis中的key:powerToHighAndLows + dataType + "Time"
         * 先判断redis中是否存在这个key
         * 存在：覆盖map中的时间为最新的时间
         * 不存在：创建这个key，存放插入时间
         */
        if (primaryRedisUtil.hasKey(powerToHighAndLows + dataType + "Time")) {
            Map<Object, Object> map = primaryRedisUtil.hmget(powerToHighAndLows + dataType + "Time");
            map.put(valueOf, new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            primaryRedisUtil.hmset(powerToHighAndLows + dataType + "Time", map);

        } else {
            Map<Object, Object> map = new HashMap<>();
            map.put(valueOf, new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            primaryRedisUtil.hmset(powerToHighAndLows + dataType + "Time", map);
        }

    }

    @Override
    public void powerMongodbByRedis() {
        log.info("开始同步电力数据到mongodb");
        //高压遥测数据
        Map<Object, Object> highRemoteDetect = primaryRedisUtil.hmget(PowerConstant.POWER_HIGH + PowerConstant.POWER_REMOTEDETECT);
        for (Object key : highRemoteDetect.keySet()) {
            Object value = highRemoteDetect.get(key);
            HighRemoteDetect remoteDetect = new HighRemoteDetect();
            remoteDetect.setRemotDetectId(Integer.parseInt(String.valueOf(key)));
            remoteDetect.setEValue(Double.valueOf(String.valueOf(value)));
            remoteDetect.setCreateDate(new Date());
            remoteDetect.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            Date startTime = new Date();
            try {
                mongoTemplate.save(remoteDetect, "yjf_e104_high_remote_detect");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }

        //高压遥信数据
        Map<Object, Object> highSinglePoint = primaryRedisUtil.hmget(PowerConstant.POWER_HIGH + PowerConstant.POWER_SINGLEPOINT);
        for (Object key : highSinglePoint.keySet()) {
            Object value = highSinglePoint.get(key);
            HighRemoteSignal remoteSignal = new HighRemoteSignal();
            remoteSignal.setRemoteSignalId(Integer.parseInt(String.valueOf(key)));
            remoteSignal.setEValue(Integer.parseInt(String.valueOf(value)));
            remoteSignal.setCreateDate(new Date());
            remoteSignal.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            Date startTime = new Date();
            try {
                mongoTemplate.save(remoteSignal, "yjf_e104_high_remote_signal");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }

        //高压电度数据
        Map<Object, Object> highProcess = primaryRedisUtil.hmget(PowerConstant.POWER_HIGH + PowerConstant.POWER_PROCESS);
        for (Object key : highProcess.keySet()) {
            Object value = highProcess.get(key);
            HighRemotePulse remotePulse = new HighRemotePulse();
            remotePulse.setRemotPulseId(Integer.parseInt(String.valueOf(key)));
            remotePulse.setEValue(Integer.parseInt(String.valueOf(value)));
            remotePulse.setCreateDate(new Date());
            remotePulse.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));

            Date startTime = new Date();
            try {
                mongoTemplate.save(remotePulse, "yjf_e104_high_remote_pulse");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }

        //低压遥测数据
        Map<Object, Object> lowRemoteDetect = primaryRedisUtil.hmget(PowerConstant.POWER_LOW + PowerConstant.POWER_REMOTEDETECT);
        for (Object key : lowRemoteDetect.keySet()) {
            Object value = lowRemoteDetect.get(key);
            LowRemoteDetect remoteDetect = new LowRemoteDetect();
            remoteDetect.setRemotDetectId(Integer.parseInt(String.valueOf(key)));
            remoteDetect.setEValue(Double.valueOf(String.valueOf(value)));
            remoteDetect.setCreateDate(new Date());
            remoteDetect.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            Date startTime = new Date();
            try {
                mongoTemplate.save(remoteDetect, "yjf_e104_low_remote_detect");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }

        //低压遥信数据
        Map<Object, Object> lowSinglePoint = primaryRedisUtil.hmget(PowerConstant.POWER_LOW + PowerConstant.POWER_SINGLEPOINT);
        for (Object key : lowSinglePoint.keySet()) {
            Object value = lowSinglePoint.get(key);
            LowRemoteSignal remoteSignal = new LowRemoteSignal();
            remoteSignal.setRemoteSignalId(Integer.parseInt(String.valueOf(key)));
            remoteSignal.setEValue(Integer.parseInt(String.valueOf(value)));
            remoteSignal.setCreateDate(new Date());
            remoteSignal.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            Date startTime = new Date();
            try {
                mongoTemplate.save(remoteSignal, "yjf_e104_low_remote_signal");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }

        //低压电度数据
        Map<Object, Object> lowProcess = primaryRedisUtil.hmget(PowerConstant.POWER_LOW + PowerConstant.POWER_PROCESS);
        for (Object key : lowProcess.keySet()) {
            Object value = lowProcess.get(key);
            LowRemotePulse remotePulse = new LowRemotePulse();
            remotePulse.setRemotPulseId(Integer.parseInt(String.valueOf(key)));
            remotePulse.setEValue(Integer.parseInt(String.valueOf(value)));
            remotePulse.setCreateDate(new Date());
            remotePulse.setPid(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
            Date startTime = new Date();
            try {
                mongoTemplate.save(remotePulse, "yjf_e104_low_remote_pulse");
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 1, "Y", "成功");
            } catch (Exception e) {
                e.printStackTrace();
                Date endTime = new Date();
                systemProcessLogService.addSystemProcessLog("电力系统_MongoDB_接入方式", "ETL", startTime, endTime, 1, 0, "W", "警告");
            }
        }
        log.info("同步电力数据到mongodb完成");
    }
}
